﻿package idv.cjcat.emitter {
	use namespace engine;
	
	import idv.cjcat.emitter.geom.*;
	
	/**
	 * Circle-shaped particle source.
	 */
	public class CircleSource extends ParticleSource {
		
		public var radius:Number;
		/**
		 * Whether the particles are spawned evenly in the emitter.
		 * <p>
		 * If set to false, particles are spawned closer to the center.
		 * </p>
		 */
		public var even:Boolean;
		
		public function CircleSource(x:Number = 0, y:Number = 0, radius:Number = 100, even:Boolean = true) {
			super(x, y);
			this.radius = radius;
			this.even = even;
		}
		
		override public function spawnPos():MotionData2D {
			var temp:MotionData2D = new MotionData2D();
			var randLength:Number = radius * ((even)?(Math.sqrt(Math.random())):(Math.random()));
			var randAng:Number = Math.random() * Math.PI * 2;
			temp.x = x + randLength * Math.cos(randAng);
			temp.y = y + randLength * Math.sin(randAng);
			return temp;
		}
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		override public function toXML():XML {
			var xml:XML = super.toXML();
			xml.@type = "circle";
			xml.@radius = radius.toString();
			xml.@even = even.toString();
			
			return xml;
		}
		override public function parseXML(xml:XML):void {
			super.parseXML(xml);
			
			radius = parseFloat(xml.@radius);
			even = (xml.@even == "true");
		}
		
		//------------------------------------------------------------------------------------------------
		//end of XML
	}
}